﻿#include "maincontractwidget.h"

MainContractWidget::MainContractWidget(QWidget *parent) :
    QWidget(parent)
{
    setupModel();
    setupViews();
}
bool MainContractWidget::eventFilter(QObject *obj, QEvent *event){
    if (event->type() == QEvent::KeyPress) {
        QKeyEvent* keyEvent = (QKeyEvent*)(event);
        emit SigkeyPressEvent(keyEvent);
        return true;
    } else {
        // standard event processing
        return QObject::eventFilter(obj, event);
    }
}

void MainContractWidget::setupModel()
{
    modelContract = new QStandardItemModel(0, 13, this);
    modelContract->setHeaderData(0, Qt::Horizontal, QStringLiteral("交易所"));
    modelContract->setHeaderData(1, Qt::Horizontal, QStringLiteral("合约代码"));
    modelContract->setHeaderData(2, Qt::Horizontal, QStringLiteral("合约名称"));
    modelContract->setHeaderData(3, Qt::Horizontal, QStringLiteral("每手吨数"));
    modelContract->setHeaderData(4, Qt::Horizontal, QStringLiteral("最小价差"));
    modelContract->setHeaderData(5, Qt::Horizontal, QStringLiteral("最小下单手数"));
    modelContract->setHeaderData(6, Qt::Horizontal, QStringLiteral("最大下单手数"));  //b/s
    modelContract->setHeaderData(7, Qt::Horizontal, QStringLiteral("买-投机保证金"));
    modelContract->setHeaderData(8, Qt::Horizontal, QStringLiteral("买-套保保证金"));
    modelContract->setHeaderData(9, Qt::Horizontal, QStringLiteral("买-套利保证金"));
    modelContract->setHeaderData(10, Qt::Horizontal, QStringLiteral("卖-投机保证金"));
    modelContract->setHeaderData(11, Qt::Horizontal, QStringLiteral("卖-套保保证金"));
    modelContract->setHeaderData(12, Qt::Horizontal, QStringLiteral("卖-套利保证金"));
}

void MainContractWidget::setupViews()
{
    MainLayout = new QGridLayout(this);

    tableContract = new QTableView;
    tableContract->setAlternatingRowColors(true);
    QFont font = tableContract->horizontalHeader()->font();
    font.setBold(true);
    tableContract->horizontalHeader()->setFont(font);

    tableContract->setModel(modelContract);
    tableContract->setEditTriggers(QAbstractItemView::NoEditTriggers);
    tableContract->verticalHeader()->setVisible(false); //隐藏列表头
    tableContract->verticalHeader()->setFixedWidth(20);
    tableContract->setSelectionBehavior(QAbstractItemView::SelectRows);
    tableContract->setSelectionMode(QAbstractItemView::SingleSelection);

    tableContract->setColumnWidth(0, 100);
    tableContract->setColumnWidth(1, 120);
    tableContract->setColumnWidth(2, 160);
    tableContract->setColumnWidth(3, 120);
    tableContract->setColumnWidth(4, 100);
    tableContract->setColumnWidth(5, 100);
    tableContract->setColumnWidth(6, 100);
    tableContract->setColumnWidth(7, 100);
    tableContract->setColumnWidth(8, 100);
    tableContract->setColumnWidth(9, 100);
    tableContract->setColumnWidth(10, 100);
    tableContract->setColumnWidth(11, 100);
    tableContract->setColumnWidth(12, 100);

    MainLayout->addWidget(tableContract,0,0);
    MainLayout->setMargin(0);
    MainLayout->setSpacing(0);

    tableContract->installEventFilter(this);
}
double MainContractWidget::GetPriceStep(QString code){
	if (m_MapPriceStep.contains(code)){
		return m_MapPriceStep[code];
	}else{
		return 0;
	}
}
/*void MainContractWidget::ShowInstrumentMarginRateInfo(IF2UnPacker *lpUnPacker){
    while(lpUnPacker->IsEOF() == 0){
        QString futucode = lpUnPacker->GetStr("futu_code");
        char bs = lpUnPacker->GetChar("entrust_bs");
        double ratio = lpUnPacker->GetDouble("open_ratio");
        char hedge = lpUnPacker->GetChar("hedge_type");

        switch (hedge) {
        case '0'://投机
            if(bs == '1'){//买入
                m_MapBuyTouji.insert(futucode,ratio);
            }else if (bs == '2'){//卖出
                m_MapSellTouji.insert(futucode,ratio);
            }
            break;
        case '1'://套保
            if(bs == '1'){//买入
                m_MapBuyTaobao.insert(futucode,ratio);
            }else if (bs == '2'){//卖出
                m_MapSellTaobao.insert(futucode,ratio);
            }
            break;
        case '2'://套利
            if(bs == '1'){//买入
                m_MapBuyTaoli.insert(futucode,ratio);
            }else if (bs == '2'){//卖出
                m_MapSellTaoli.insert(futucode,ratio);
            }
            break;
        default:
            break;
        }
        lpUnPacker->Next();
    }
}*/
void MainContractWidget::ShowInstrumentMarginRateInfo(){
    int iRow = modelContract->rowCount();
    for(int i = 0; i < iRow; i++){
        QString futucode = ((QStandardItemModel*) modelContract)->item(i,1)->text();
        //买-投机保证金
        if(m_MapBuyTouji.contains(futucode)){
            ((QStandardItemModel*) modelContract)->setItem(i, 7, new QStandardItem(QString::number(m_MapBuyTouji[futucode])));
        }
        //买-套保保证金
        if(m_MapBuyTaobao.contains(futucode)){
            ((QStandardItemModel*) modelContract)->setItem(i, 8, new QStandardItem(QString::number(m_MapBuyTaobao[futucode])));
        }
        //买-套利保证金
        if(m_MapBuyTaoli.contains(futucode)){
            ((QStandardItemModel*) modelContract)->setItem(i, 9, new QStandardItem(QString::number(m_MapBuyTaoli[futucode])));
        }
        //卖-投机保证金
        if(m_MapSellTouji.contains(futucode)){
            ((QStandardItemModel*) modelContract)->setItem(i, 10, new QStandardItem(QString::number(m_MapSellTouji[futucode])));
        }
        //卖-套保保证金
        if(m_MapSellTaobao.contains(futucode)){
            ((QStandardItemModel*) modelContract)->setItem(i, 11, new QStandardItem(QString::number(m_MapSellTaobao[futucode])));
        }
        //卖-套利保证金
        if(m_MapSellTaoli.contains(futucode)){
            ((QStandardItemModel*) modelContract)->setItem(i, 12, new QStandardItem(QString::number(m_MapSellTaoli[futucode])));
        }
    }
    tableContract->reset();
}

void MainContractWidget::AddRow(CThostFtdcInstrumentField *pInstrument){

    QString contractCode =  QString(pInstrument->InstrumentID);
    double  pricestep    = pInstrument->PriceTick;
    double  amount_per_hand = pInstrument->VolumeMultiple;

    m_MapPriceStep.insert(contractCode,pricestep);
    m_MapAmountPerHand.insert(contractCode,amount_per_hand);

    int iRow = modelContract->rowCount();
    modelContract->insertRow(iRow);
    //交易所
    ((QStandardItemModel*) modelContract)->setItem(iRow, 0, new QStandardItem(QString(pInstrument->ExchangeID)));
    //合约代码
    ((QStandardItemModel*) modelContract)->setItem(iRow, 1, new QStandardItem(pInstrument->InstrumentID));
    //合约名称
    QString strInstrumentName = QString::fromLocal8Bit(pInstrument->InstrumentName);
    ((QStandardItemModel*) modelContract)->setItem(iRow, 2, new QStandardItem(strInstrumentName));
    //每手吨数
    ((QStandardItemModel*) modelContract)->setItem(iRow, 3, new QStandardItem(QString::number(amount_per_hand)));
    //最小价差
    ((QStandardItemModel*) modelContract)->setItem(iRow, 4, new QStandardItem(QString::number(pricestep)));
    //最小下单手数
    ((QStandardItemModel*) modelContract)->setItem(iRow, 5, new QStandardItem(QString::number(pInstrument->MinLimitOrderVolume)));
    //最大下单手数
    ((QStandardItemModel*) modelContract)->setItem(iRow, 6, new QStandardItem(QString::number(pInstrument->MaxLimitOrderVolume)));
    //开仓保证金
    //((QStandardItemModel*) modelContract)->setItem(0, 7, new QStandardItem(QString(lpUnPacker->GetStr("min_margin"))));
    //合约类型
    /*if(pInstrument->ProductClass == THOST_FTDC_PC_Futures){///期货
        ((QStandardItemModel*) modelContract)->setItem(iRow, 8, new QStandardItem(QStringLiteral("期货")));
    }else if(pInstrument->ProductClass == THOST_FTDC_PC_Options){///期货期权
        ((QStandardItemModel*) modelContract)->setItem(iRow, 8, new QStandardItem(QStringLiteral("期货期权")));
    }else if(pInstrument->ProductClass == THOST_FTDC_PC_Combination){///组合
        ((QStandardItemModel*) modelContract)->setItem(iRow, 8, new QStandardItem(QStringLiteral("组合")));
    }else if(pInstrument->ProductClass == THOST_FTDC_PC_Spot){///即期
        ((QStandardItemModel*) modelContract)->setItem(iRow, 8, new QStandardItem(QStringLiteral("即期")));
    }else if(pInstrument->ProductClass == THOST_FTDC_PC_EFP){///期转现
        ((QStandardItemModel*) modelContract)->setItem(iRow, 8, new QStandardItem(QStringLiteral("期转现")));
    }else if(pInstrument->ProductClass == THOST_FTDC_PC_SpotOption){///现货期权
        ((QStandardItemModel*) modelContract)->setItem(iRow, 8, new QStandardItem(QStringLiteral("现货期权")));
    }else{
        ((QStandardItemModel*) modelContract)->setItem(iRow, 8, new QStandardItem(QStringLiteral("未知")));
    }*/

    //tableContract->reset();
}
int    MainContractWidget::GetAmountPerHand(QString futucode){
    if(m_MapAmountPerHand.contains(futucode)){
        return m_MapAmountPerHand[futucode];
    }
    return 0;
}
double MainContractWidget::GetOpenRatio(char bs,QString ordertype,QString futucode){

    double ratio = 0;
    switch(bs){
    case '1':
        if(ordertype == QStringLiteral("限价") || ordertype == QStringLiteral("市价")){
            if(m_MapBuyTouji.contains(futucode)){
                ratio = m_MapBuyTouji[futucode];
            }
        }else if(ordertype == QStringLiteral("套保")){
            if(m_MapBuyTaobao.contains(futucode)){
                ratio = m_MapBuyTaobao[futucode];
            }
        }else if(ordertype == QStringLiteral("套利")){
            if(m_MapBuyTaoli.contains(futucode)){
                ratio = m_MapBuyTaoli[futucode];
            }
        }
        break;
    case '2':
        if(ordertype == QStringLiteral("限价") || ordertype == QStringLiteral("市价")){
            if(m_MapSellTouji.contains(futucode)){
                ratio = m_MapSellTouji[futucode];
            }
        }else if(ordertype == QStringLiteral("套保")){
            if(m_MapSellTaobao.contains(futucode)){
                ratio = m_MapSellTaobao[futucode];
            }
        }else if(ordertype == QStringLiteral("套利")){
            if(m_MapSellTaoli.contains(futucode)){
                ratio = m_MapSellTaoli[futucode];
            }
        }
        break;
    }
    return ratio;
}
